/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.data;

import java.util.Hashtable;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import org.kalmeo.util.BooleanUtil;

/**
 *
 * @author Marnix Renne
 */
public final class PropertyRecordStore {

    private Hashtable recordIndexMap;
    private RecordStore recordStore;

    public PropertyRecordStore(String name) throws RecordStoreException {
        super();
        recordStore = RecordStore.openRecordStore(name, true);
        recordIndexMap = new Hashtable();
    }

    public void setIntProperty(String key, int value) {

    }

    public int getIntProperty(String key, int defaultValue) {
        String result = getProperty(key);
        if (result != null) {
            return Integer.valueOf(result).intValue();
        }
        return defaultValue;
    }

    public void setStringProperty(String key, String value) {

    }

    public String getStringProperty(String key, String defaultValue) {
        String result = getProperty(key);
        if (result != null) {
            return result;
        }
        return defaultValue;
    }

    public void setBooleanProperty(String key, boolean value) {

    }

    public boolean getBooleanProperty(String key, boolean defaultValue) {
        String result = getProperty(key);
        if (result != null) {
            return BooleanUtil.parseBoolean(result);
        }
        return defaultValue;
    }

    protected String getProperty(String key) {
        int index = getIndex(key);

        if (index >= 0) {
            try {
                byte[] data = recordStore.getRecord(index);
                String dataString = String.valueOf(data);

                return dataString.substring(dataString.indexOf("="));
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    protected void setProperty(String key, String value) {
        int index = getIndex(key);
        byte[] data = (key + "=" + value).getBytes();
        try {
            if (index >= 0) {
                recordStore.setRecord(index, data, 0, data.length);
            } else {
                index = recordStore.addRecord(data, 0, data.length);
                recordIndexMap.put(key, new Integer(index));
            }
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (InvalidRecordIDException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    private int getIndex(String key) {
        Integer index = (Integer) recordIndexMap.get(key);
        if (index == null) {
            try {
                RecordEnumeration e = recordStore.enumerateRecords(null, null, false);
                while (e.hasNextElement()) {
                    byte[] data = e.nextRecord();
                    String dataString = String.valueOf(data);
                    if (dataString.startsWith(key + "=")) {
                        int id = e.previousRecordId(); // return previous record, since enum is already at next record
                        recordIndexMap.put(key, new Integer(id));
                        return id;
                    }
                }
            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        return -1;
    }
}
